package convert.converter;

import org.yaml.snakeyaml.Yaml;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.FileInputStream;
import java.util.Map;



public class yaml_to_xml {

    public static void main(String[] args) {
        String yamlFilePath = "src/resource/yaml/output.yaml";
        String xmlFilePath = "src/resource/xml/yaml_output.xml";

        convertYAMLToXML(yamlFilePath, xmlFilePath);
    }

    public static void convertYAMLToXML(String yamlFilePath, String xmlFilePath) {
        try {
            InputStream inputStream = new FileInputStream(yamlFilePath);
            InputStreamReader reader = new InputStreamReader(inputStream);
            Yaml yaml = new Yaml();
            Iterable<Object> data = yaml.loadAll(reader);

            DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
            Document doc = docBuilder.newDocument();

            for (Object item : data) {
                Element element = createXMLElement(doc, item);
                doc.appendChild(element);
            }

            FileWriter writer = new FileWriter(xmlFilePath);
            writeXMLToFile(doc, writer);
            writer.close();

            reader.close();
            inputStream.close();

            System.out.println("YAML to XML conversion completed successfully.");
        } catch (IOException | ParserConfigurationException e) {
            e.printStackTrace();
        }
    }

    private static Element createXMLElement(Document doc, Object item) {
        Element element = doc.createElement("item");
        if (item instanceof String) {
            element.setTextContent((String) item);
        } else if (item instanceof Iterable) {
            for (Object subItem : (Iterable<?>) item) {
                Element subElement = createXMLElement(doc, subItem);
                element.appendChild(subElement);
            }
        } else if (item instanceof Map) {
            for (Map.Entry<?, ?> entry : ((Map<?, ?>) item).entrySet()) {
                Element subElement = doc.createElement(String.valueOf(entry.getKey()));
                Object value = entry.getValue();
                if (value instanceof String) {
                    subElement.setTextContent((String) value);
                } else {
                    Element subItemElement = createXMLElement(doc, value);
                    subElement.appendChild(subItemElement);
                }
                element.appendChild(subElement);
            }
        }
        return element;
    }

    private static void writeXMLToFile(Document doc, FileWriter writer) throws IOException {
        javax.xml.transform.TransformerFactory transformerFactory =
                javax.xml.transform.TransformerFactory.newInstance();
        try {
            javax.xml.transform.Transformer transformer = transformerFactory.newTransformer();
            transformer.setOutputProperty(javax.xml.transform.OutputKeys.INDENT, "yes");
            javax.xml.transform.dom.DOMSource source = new javax.xml.transform.dom.DOMSource(doc);
            javax.xml.transform.stream.StreamResult result =
                    new javax.xml.transform.stream.StreamResult(writer);
            transformer.transform(source, result);
        } catch (javax.xml.transform.TransformerException e) {
            e.printStackTrace();
        }
    }
}
